package com.zjy.pgcms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjy.pgcms.common.BaseResponse;
import com.zjy.pgcms.common.ErrorCode;
import com.zjy.pgcms.common.Result;
import com.zjy.pgcms.dto.project.ProjectDTO;
import com.zjy.pgcms.entity.Project;
import com.zjy.pgcms.exception.BusinessException;
import com.zjy.pgcms.service.ProjectService;
import com.zjy.pgcms.util.FileUtil;
import com.zjy.pgcms.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class ProjectController {
    @Resource
    private ProjectService projectService;

    @Value("${staticFile.path}")
    private String path;

    @Value("${staticFile.detail-picture}")
    private String detailPicturePath;

    @GetMapping("/project/list")
    public BaseResponse getAllProject(){
        List<Project> list = projectService.list();
        return Result.success(list);
    }

    @GetMapping("/get/project/{id}")
    public BaseResponse getProjectById(@PathVariable Long id){
        QueryWrapper<Project> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",id);
        Project project = projectService.getOne(objectQueryWrapper);
        return Result.success(project);
    }

    @DeleteMapping("/project/delete/{id}")
    public BaseResponse deleteProjectById(@PathVariable Long id){
        projectService.removeById(id);
        return Result.success();
    }

    @PostMapping("/project/add")
    public BaseResponse addPetServeProject(HttpServletRequest request, ProjectDTO dto){
        if (ValidationUtil.hasNotLogin(request)){
            throw new BusinessException(ErrorCode.NO_LOGIN);
        }
        MultipartFile headPicture = dto.getFileHead();
        MultipartFile detailPicture = dto.getFileDetail();
        Project project = projectService.getInstance(request, dto);
        boolean save = projectService.save(project);
        if (save){
            String headPictureUrlNet = FileUtil.saveAvatar(headPicture, path, project.getId());
            project.setHeadPictureUrl(headPictureUrlNet);
            String detailPictureUrlNet = FileUtil.saveAvatar(detailPicture, path, project.getId());
            project.setDetailPictureUrl(detailPictureUrlNet);
            projectService.updateById(project);
        }
        return Result.success();
    }

    @PostMapping("/project/update")
    public BaseResponse updateServeProject(HttpServletRequest request,ProjectDTO dto){
        MultipartFile headPicture = dto.getFileHead();
        MultipartFile detailPicture = dto.getFileDetail();

        Project project = projectService.getInstance(request, dto);

        project.setId(dto.getId());

        String headPictureUrlNet = FileUtil.saveAvatar(headPicture, path, project.getId());
        project.setHeadPictureUrl(headPictureUrlNet);

        String detailPictureUrlNet = FileUtil.saveAvatar(detailPicture, path, project.getId());
        project.setDetailPictureUrl(detailPictureUrlNet);


        projectService.updateById(project);
        return Result.success();
    }


}
